package com.qingxin.park.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.park.service.CompanyService;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.park.service.ParkBlockService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.SysGroup;
import com.qingxin.sys.service.SysGroupService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;

/**
 * 园区树-jstree版
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/parkjstree")
public class ParkJstreeAction extends BaseAction {

	@Autowired
	private FarmParkService farmParkService;
	@Autowired
	private ParkBlockService parkBlockService;
	@Autowired
	private SysGroupService sysGroupService;
	
	@Autowired
	private CompanyService companyService;
	
	/**
	 * 加载框架左侧园区-地块导航树数据
	 * @param module
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/menudata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object menudata(String module, String pdatatype, String pid, HttpServletRequest request){
		//园区管理
		if("park".equalsIgnoreCase(module)){
			return loadParkModule(pdatatype, pid, request);
		}
		//产地环境管理
		if("prov".equalsIgnoreCase(module)){
			return loadProvModule(pdatatype, pid, request);
		}
		//名优产品管理
		if("well".equalsIgnoreCase(module)){
			return loadWellModule(pdatatype, pid, request);
		}
		
		//园区分布地图
		if("map".equalsIgnoreCase(module)){
			return loadMapModule(pdatatype, pid, request);
		}
		
		//园区视频地图
		if("video".equalsIgnoreCase(module)){
			return loadVideoModule(pdatatype, pid, request);
		}

		return loadCommon(module, pdatatype, pid, request);
	}
	
	
	@RequestMapping(value="/groupdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object groupdata(String module, String lefttab,
			String pdatatype, String pid, HttpServletRequest request){
		//园区管理
		if("park".equalsIgnoreCase(module)){
			return loadParkModuleByGroup(pdatatype, pid, request);
		}
		
		//园区分布地图
		if("map".equalsIgnoreCase(module)){
			return loadMapModuleByGroup(pdatatype, pid, request);
		}
		
		return loadCommonByGroup(module, lefttab, pdatatype, pid, request);
	}
	
	
	/**
	 * 按地块用途类型加载园区数据
	 * @param module
	 * @param pdatatype
	 * @param pid
	 * @param blockType
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/menudatatype.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object menudatatype(String module, String pdatatype, String pid, String blockType, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		LoginUser loginUser = getLoginUser(request);
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			List<TreeNode> tlist = farmParkService.getUITreeDataType(loginUser, null, true);
			for(TreeNode treeNode : tlist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, true));
			}
		}
		else if(SystemCfg.UI_DATATYPE_PARK.equalsIgnoreCase(pdatatype)){
			List<TreeNode> tlist = parkBlockService.getUITreeDataType(pid, blockType, false, loginUser);
			for(TreeNode treeNode : tlist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, false));
			}
		}
		return list;
	}
	
	
	@RequestMapping(value="/menudatacompany.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object menudatacompany(String module, String pdatatype, String pid, String blockType, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		if("products".equalsIgnoreCase(module)){
			if(StringUtils.isEmpty(pid) || "#".equals(pid)){
				List<TreeNode> clist = companyService.getCompanyData(loginUser);
				List<JsTreeNode> list = new ArrayList<>();
				for(TreeNode treeNode : clist){
					list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "COMP", null, true));
				}
				return list;
			}else if("COMP".equals(pdatatype)){
				//String companyid = null;
				List<TreeNode> clist = farmParkService.getUITreeDataCompany(pid, null, loginUser, false);
				List<JsTreeNode> list = new ArrayList<>();
				for(TreeNode treeNode : clist){
					list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "PARK", null, true));
				}
				return list;
			}else if("PARK".equals(pdatatype)){
				List<TreeNode> clist = parkBlockService.getUITreeDataType(pid, blockType,false, loginUser);
				List<JsTreeNode> list = new ArrayList<>();
				for(TreeNode treeNode : clist){
					list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "BLOCK", null, false));
				}
				return list;
			}
		}
		
		return new ArrayList<Object>();
	}
	
	
	@RequestMapping(value="/menudatadetection.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object menudatadetection(String module, String pdatatype, String pid, String blockType, HttpServletRequest request){
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			List<TreeNode> clist = companyService.getAllCompanyData();
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : clist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "COMP", null, true));
			}
			return list;
		}else if("COMP".equals(pdatatype)){
			//String companyid = null;
			List<TreeNode> clist = farmParkService.getUITreeDataCompanyAll(pid);
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : clist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "PARK", null, true));
			}
			return list;
		}else if("PARK".equals(pdatatype)){
			List<TreeNode> clist = parkBlockService.getUITreeDataParkAll(pid);
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : clist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "BLOCK", null, false));
			}
			return list;
		}
		return new ArrayList<Object>();
	}
	
	
	
	/**
	 * 园区管理
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<JsTreeNode> loadParkModule(String pdatatype, String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		LoginUser loginUser = getLoginUser(request);
		List<TreeNode> tlist = farmParkService.getUITreeData(loginUser, false);
		for(TreeNode treeNode : tlist){
			list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, false));
		}
		return list;
	}
	
	
	/**
	 * 产地环境管理
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<JsTreeNode> loadProvModule(String pdatatype, String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		LoginUser loginUser = getLoginUser(request);
		
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			JsTreeNode root = new JsTreeNode("0", "所有园区", "GROUP", null, true);
			list.add(root);
		}
		else if("GROUP".equalsIgnoreCase(pdatatype)){
			List<TreeNode> tlist = farmParkService.getUITreeData(loginUser, false);
			for(TreeNode treeNode : tlist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, false));
			}
		}
		else if(SystemCfg.UI_DATATYPE_PARK.equalsIgnoreCase(pdatatype)){
			//list.addAll(parkBlockService.getTreeData(pid, false, loginUser));
		}
		return list;
	}
	
	/**
	 *名优产品管理
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<JsTreeNode> loadWellModule(String pdatatype, String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		LoginUser loginUser = getLoginUser(request);
		
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			JsTreeNode root = new JsTreeNode("0", "所有园区", "GROUP", null, true);
			list.add(root);
		}
		else if("GROUP".equalsIgnoreCase(pdatatype)){
			List<TreeNode> tlist = farmParkService.getUITreeData(loginUser, false);
			for(TreeNode treeNode : tlist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, false));
			}
		}
		else if(SystemCfg.UI_DATATYPE_PARK.equalsIgnoreCase(pdatatype)){
			//list.addAll(parkBlockService.getTreeData(pid, false, loginUser));
		}
		return list;
	}
	
	
	/**
	 *视频管理
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<JsTreeNode> loadVideoModule(String pdatatype, String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		LoginUser loginUser = getLoginUser(request);
		
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			JsTreeNode root = new JsTreeNode("0", "所有园区", "GROUP", null, true);
			list.add(root);
		}
		else if("GROUP".equalsIgnoreCase(pdatatype)){
			List<TreeNode> tlist = farmParkService.getUITreeData(loginUser, false);
			for(TreeNode treeNode : tlist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, false));
			}
		}
		else if(SystemCfg.UI_DATATYPE_PARK.equalsIgnoreCase(pdatatype)){
			//list.addAll(parkBlockService.getTreeData(pid, false, loginUser));
		}
		return list;
	}
	/**
	 * 园区管理-按分组
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<JsTreeNode> loadParkModuleByGroup(String pdatatype, String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		LoginUser loginUser = getLoginUser(request);
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			if(SystemCfg.isAdminUser(loginUser)){
				List<TreeNode> glist = sysGroupService.getGroupTree("0", "GROUP", null, loginUser);
				for(TreeNode treeNode : glist){
					list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "GROUP", "groupid", true));
				}
				return list;
			}else {
				String groupid = loginUser.getGroupid();
				if(StringUtils.isNotEmpty(groupid)){
					SysGroup group = sysGroupService.get(groupid);
					list.add(new JsTreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true));
				}
				return list;
			}
		}else{
			if("GROUP".equalsIgnoreCase(pdatatype)){
				List<TreeNode> glist = sysGroupService.getGroupTree(pid, "GROUP", null, loginUser);
				for(TreeNode treeNode : glist){
					list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "GROUP", "groupid", true));
				}
				List<TreeNode> tlist = farmParkService.getUITreeData(pid, loginUser, false);
				for(TreeNode treeNode : tlist){
					list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, false));
				}
				return list;
			}
		}
		return list;
	}
	
	
	/**
	 * 园区分布地图
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<JsTreeNode> loadMapModule(String pdatatype, String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		LoginUser loginUser = getLoginUser(request);
		
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			JsTreeNode root = new JsTreeNode("0", "所有园区", "GROUP", null, true);
			list.add(root);
		}
		else if("GROUP".equalsIgnoreCase(pdatatype)){
			List<TreeNode> tlist = farmParkService.getUITreeData(null, loginUser, false);
			for(TreeNode treeNode : tlist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, false));
			}
		}
		return list;
		
	}
	
	
	/**
	 * 园区分布地图-按分组
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<JsTreeNode> loadMapModuleByGroup(String pdatatype, String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		LoginUser loginUser = getLoginUser(request);
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			if(SystemCfg.isAdminUser(loginUser)){
				List<TreeNode> glist = sysGroupService.getGroupTree("0", "GROUP", null, loginUser);
				for(TreeNode treeNode : glist){
					list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "GROUP", "groupid", true));
				}
				return list;
			}else {
				String groupid = loginUser.getGroupid();
				if(StringUtils.isNotEmpty(groupid)){
					SysGroup group = sysGroupService.get(groupid);
					list.add(new JsTreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true));
				}
				return list;
			}
		}else{
			if("GROUP".equalsIgnoreCase(pdatatype)){
				List<TreeNode> tlist = farmParkService.getUITreeData(pid, loginUser, false);
				for(TreeNode treeNode : tlist){
					list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, false));
				}
			}
		}
		return list;
	}
	
	
	/**
	 * 通用导航菜单数据
	 * @param module
	 * @param lefttab
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<JsTreeNode> loadCommon(String module, String pdatatype, String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		LoginUser loginUser = getLoginUser(request);
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			List<TreeNode> tlist = farmParkService.getUITreeData(null, loginUser, true);
			for(TreeNode treeNode : tlist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, true));
			}
		}
		else if(SystemCfg.UI_DATATYPE_PARK.equalsIgnoreCase(pdatatype)){
			List<TreeNode> tlist = parkBlockService.getUITreeData(pid, false, loginUser);
			for(TreeNode treeNode : tlist){
				list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, false));
			}
		}
		return list;
	}
	
	
	/**
	 * 通用导航菜单数据-按分组
	 * @param module
	 * @param lefttab
	 * @param pdatatype
	 * @param pid
	 * @param request
	 * @return
	 */
	private List<JsTreeNode> loadCommonByGroup(String module, String lefttab, String pdatatype, String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		LoginUser loginUser = getLoginUser(request);
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			if(SystemCfg.isAdminUser(loginUser)){
				List<TreeNode> glist = sysGroupService.getGroupTree("0", "GROUP", null, loginUser);
				for(TreeNode treeNode : glist){
					list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "GROUP", "groupid", true));
				}
				return list;
			}else {
				String groupid = loginUser.getGroupid();
				if(StringUtils.isNotEmpty(groupid)){
					SysGroup group = sysGroupService.get(groupid);
					list.add(new JsTreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true));
				}
				return list;
			}
		}else{
			if("GROUP".equalsIgnoreCase(pdatatype)){
				List<TreeNode> glist = sysGroupService.getGroupTree(pid, "GROUP", null, loginUser);
				for(TreeNode treeNode : glist){
					list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), "GROUP", "groupid", true));
				}
				List<TreeNode> tlist = farmParkService.getUITreeData(pid, loginUser, false);
				for(TreeNode treeNode : tlist){
					list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, false));
				}
				return list;
			}else if(SystemCfg.UI_DATATYPE_PARK.equalsIgnoreCase(pdatatype)){
				List<TreeNode> tlist = parkBlockService.getUITreeData(pid, false, loginUser);
				for(TreeNode treeNode : tlist){
					list.add(new JsTreeNode(treeNode.getId(), treeNode.getName(), treeNode.getType(), null, false));
				}
			}
		}
		return list;
	}
	
}
